/***************************************************************************
创建者：华磊
 开始时间：         : 2020.11.10
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
 *   程序文件主视窗
*                                                                         *
 ***************************************************************************/
#include "programfileview.h"
#include "ui_programfileview.h"
#include <QStringList>
#include "hmicommondefine.h"
#include "instructionEnum.h"


extern FileData fileData;

//对类成员变量进行初始化
ProgramFileView::ProgramFileView(int styleId,GlobalData *globalDataIn, QWidget *parent):
        QDialog(parent),
        ui(new Ui::ProgramFileView)
{
    globalData = globalDataIn;
    ui->setupUi(this);
    setGeometry(70,128,460,595);
    this->styleId = styleId;
    initialDisplayStyle(styleId);


    fileString << "";
//    remoteProgramFileList << "";

    ui->fileName_lineEdit->setFocusPolicy(Qt::NoFocus);
    ui->version_lineEdit->setFocusPolicy(Qt::NoFocus);
    ui->owner_lineEdit->setFocusPolicy(Qt::NoFocus);
    ui->createTime_lineEdit->setFocusPolicy(Qt::NoFocus);
    ui->note_textEdit->setFocusPolicy(Qt::NoFocus);

    ui->RemoteFile_TableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);//不可编辑
    ui->RemoteFile_TableWidget->setColumnCount(1);
    ui->RemoteFile_TableWidget->setColumnWidth(0, 440);


    //*******文件列表****************************
    if(GSOAP_OPERATE_PROGRAM_FILE_SWITCH)
    {
        ui->file_treeView->hide();
        ui->label_5->hide();
        ui->label_7->hide();

        // 设置单行选择
        ui->RemoteFile_TableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
        // 设置不可编辑
        ui->RemoteFile_TableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
        // 设置表头颜色
        ui->RemoteFile_TableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section {\
            background-color:rgb(26, 174, 222);\
            color: white;\
            padding-left: 4px;\
            border: 1px solid #1aaede;}");

    }
    else
    {
        ui->RemoteFile_TableWidget->hide();

        dirModel = new QFileSystemModel;
        //中文标头
        dirModel->setHeaderData(0,Qt::Horizontal,"文件名");
        dirModel->setHeaderData(1,Qt::Horizontal,"大小");
        //设置排序方式
        //index = dirModel->setRootPath(PROGRAM_PATH);    //路径
        index = dirModel->setRootPath("/");    //路径:存在情况如果文件夹本身打不了怎么办,程序就启动不起了

        QStringList filter;
        filter << "*.ini" << "*.txt" << "*.xml";        //过滤器
        dirModel->setNameFilters(filter);

        //false为隐藏
        dirModel->setNameFilterDisables(false);

        ui->file_treeView->setAnimated(false);
        ui->file_treeView->setSortingEnabled(true);
        ui->file_treeView->setModel(dirModel);          //树界面设置
        ui->file_treeView->setRootIndex(index);
        ui->file_treeView->expand(index);               //当前项展开
        ui->file_treeView->scrollTo(index);             //定位到当前项
        ui->file_treeView->resizeColumnToContents(0);


        ui->file_treeView->header()->hide();
        ui->file_treeView->setColumnHidden(2,true);
        ui->file_treeView->setColumnHidden(3,true);
        ui->file_treeView->setColumnWidth(0,300);
    }


    //******************************************
    fileProperty.fileName = "";
    fileProperty.version = "";
    fileProperty.owner = "";
    fileProperty.createTime = "";
    fileProperty.modifyTime = "";
    fileProperty.note = "";
    fileProperty.type = "";

    newfileView = new NewFileView(styleId);
    settingProperty = new SettingProperty(styleId);
    copySettingDialog = new CopySettingDialog;
    deleteDialog = new DeleteDialog;

    ui->newFileBtn->setText(tr("新建"));
    ui->openFileBtn->setText(tr("打开"));
    ui->delFileBtn->setText(tr("删除"));
    ui->copyFileBtn->setText(tr("复制"));
    ui->defaultFileSetBtn->setText(tr("缺省设定"));
    ui->setFileProBtn->setText(tr("设置属性"));

    newfileView->setParent(this);
    settingProperty->setParent(this);
    newfileView->hide();
    settingProperty->hide();
    copySettingDialog->hide();
    deleteDialog->hide();
}

ProgramFileView::~ProgramFileView()
{
    delete ui;
}

void ProgramFileView::showThisDialog()
{

    show();
    if(0==remoteProgramFileList.size())
    {
        emit updateProgramFileList_Signal();
    }
}

void ProgramFileView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        this->setGeometry(0,0,600,450);
        ui->groupBox->setGeometry(0,280,450,160);
        ui->note_textEdit->setGeometry(80,120,350,35);

        ui->newFileBtn->setGeometry(470,20,50,45);
        ui->newFileBtn->setParent(this);
        ui->openFileBtn->setGeometry(540,20,50,45);
        ui->openFileBtn->setParent(this);
        ui->delFileBtn->setGeometry(470,20+60,50,45);
        ui->delFileBtn->setParent(this);
        ui->copyFileBtn->setGeometry(540,80,50,45);
        ui->copyFileBtn->setParent(this);

        ui->defaultFileSetBtn->setGeometry(470,140,120,45);
        ui->defaultFileSetBtn->setParent(this);
        ui->setFileProBtn->setGeometry(470,200,120,45);
        ui->setFileProBtn->setParent(this);

        break;
    default:
        break;
    }
}

void ProgramFileView::mountFileUpdate()
{
    // 更新显示程序文件列表
    if(globalData)
    {
        /*****************************************************
         * 修改人：     hualei
         * 修改时间：  2018-11-17
         * 修改说明:  增加使用GSOAP获取远程程序文件列表并显示至界面
         *
         * ***************************************************/
        // gsoap显示远程文件列表
        if(GSOAP_OPERATE_PROGRAM_FILE_SWITCH)
        {
            ui->RemoteFile_TableWidget->clear();
            ui->RemoteFile_TableWidget->setRowCount(remoteProgramFileList.size());

            QStringList tableHeadStr;
            tableHeadStr << tr("程序文件列表");
            ui->RemoteFile_TableWidget->setHorizontalHeaderLabels(tableHeadStr);

<<<<<<< HEAD
        ui->file_treeView->setAnimated(false);
        ui->file_treeView->setSortingEnabled(true);
        ui->file_treeView->setModel(dirModel);          //树界面设置
        ui->file_treeView->setRootIndex(index);
        ui->file_treeView->expand(index);               //当前项展开
        ui->file_treeView->scrollTo(index);             //定位到当前项
        ui->file_treeView->resizeColumnToContents(0);
        ui->file_treeView->setColumnWidth(0,300);
=======
            // 更新显示远程文件列表
            for(int i=0; i < remoteProgramFileList.size(); i++)
            {
                ui->RemoteFile_TableWidget->setItem(i,0, new QTableWidgetItem(remoteProgramFileList.at(i)));
            }
        }
        // 显示本地文件列表
        else
        {
            if(dirModel)
            {
                delete dirModel;
                dirModel = new QFileSystemModel;
            }

            //设置排序方式
            index = dirModel->index(PROGRAM_PATH);
            ui->file_treeView->show();
            ui->RemoteFile_TableWidget->hide();
            //中文标头
            dirModel->setHeaderData(0,Qt::Horizontal,"文件名");
            dirModel->setHeaderData(1,Qt::Horizontal,"大小");

            index = dirModel->setRootPath(fileData.currentRobotFileAddress+"program/");
            qDebug()<<"setRootPath:"<<fileData.currentRobotFileAddress+"program/";

            QStringList filter;
            filter << "*.ini" << "*.txt" << "*.xml";        //过滤器
            dirModel->setNameFilters(filter);

            //false为隐藏
            dirModel->setNameFilterDisables(false);
            ui->file_treeView->setAnimated(false);
            ui->file_treeView->setSortingEnabled(true);
            ui->file_treeView->setModel(dirModel);          //树界面设置
            ui->file_treeView->setRootIndex(index);
            ui->file_treeView->expand(index);               //当前项展开
            ui->file_treeView->scrollTo(index);             //定位到当前项
            ui->file_treeView->resizeColumnToContents(0);
            ui->file_treeView->setColumnWidth(0,300);
        }
>>>>>>> dev
    }
}

/**
 * @brief ProgramFileView::updateView更新数据到控件上
 */
void ProgramFileView::updateView()
{
    // 更新文件名
    ui->fileName_lineEdit->setText(fileProperty.fileName);

    // 更新文件版本信息
    ui->version_lineEdit->setText(fileProperty.version);

    // 更新文件所有者所有者
    ui->owner_lineEdit->setText(fileProperty.owner);
    QStringList list = fileProperty.createTime.split(" ");
    if(list.size() == 2)
    {
        ui->createTime_lineEdit->setText(list[0]);
    }
    ui->note_textEdit->setText(fileProperty.note);
}


void ProgramFileView::on_newFileBtn_clicked()
{
    //emit newFile();
    if(globalData)
    {
//        if(globalData->tryLock(TRYLOCKTIME))
//        {
            if(globalData->getUserInfo().account < EM_ACCOUNT_ADMIN)
            {
//                globalData->unlock();
                emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("您当前用户没有操作权限\n请切换用户角色"));
                return ;
            }

            if(globalData->globalCurrentState == ENUM_COMMAND_RUN || globalData->globalCurrentState == ENUM_COMMAND_CEASE )
            {
                //qDebug()<<"globalData->globalCurrentState = "<<globalData->globalCurrentState;
//                globalData->unlock();
                emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("机器程序正在运行中\n请点击停止按钮后再操作文件"));
                return ;
            }

//            globalData->unlock();
//        }
    }

    newfileView->clearLineView();
    newfileView->setGeometry(0,0,460,595);
    newfileView->show();
}

void ProgramFileView::on_openFileBtn_clicked()
{
    if(GSOAP_OPERATE_PROGRAM_FILE_SWITCH)
    {
        fileProperty.fileName = currentSelectFileName;
    }
    else
    {
        index = ui->file_treeView->currentIndex();
        fileProperty.fileName = dirModel->fileInfo(index).fileName();
    }

    emit openFile(fileProperty.fileName);
}

// 选中文件后，点击删除触发
void ProgramFileView::on_delFileBtn_clicked()
{
    if(GSOAP_OPERATE_PROGRAM_FILE_SWITCH)
    {
        fileProperty.fileName = currentSelectFileName;
    }
    else
    {
        index = ui->file_treeView->currentIndex();
        fileProperty.fileName = dirModel->fileInfo(index).fileName();
    }

    deleteDialog->initFileName(fileProperty.fileName);
    deleteDialog->show();
    //emit deleteFile(fileProperty.fileName);
}

// 选中文件后，点击复制触发
void ProgramFileView::on_copyFileBtn_clicked()
{
    if(GSOAP_OPERATE_PROGRAM_FILE_SWITCH)
    {
        fileProperty.fileName = currentSelectFileName;
    }
    else
    {
        index = ui->file_treeView->currentIndex();
        fileProperty.fileName = dirModel->fileInfo(index).fileName();
    }

    copySettingDialog->beforeName = fileProperty.fileName;
    copySettingDialog->newName = "";

    copySettingDialog->initView();//初始化
    copySettingDialog->show();

    //emit copyFile(fileProperty.fileName);
}

// 选中文件后，点击设置缺省设定按钮触发
void ProgramFileView::on_defaultFileSetBtn_clicked()
{
    QString setDefaultName;
    if(GSOAP_OPERATE_PROGRAM_FILE_SWITCH)
    {
        setDefaultName = currentSelectFileName;
    }
    else
    {
        index = ui->file_treeView->currentIndex();
        setDefaultName = dirModel->fileInfo(index).fileName();
    }

    emit defaultSetting(setDefaultName);

}

// 选中文件后,点击设置属性按钮触发
void ProgramFileView::on_setFileProBtn_clicked()
{   
    if(GSOAP_OPERATE_PROGRAM_FILE_SWITCH)
    {
        fileProperty.fileName = currentSelectFileName;
    }
    else
    {
        index = ui->file_treeView->currentIndex();
        fileProperty.fileName = dirModel->fileInfo(index).fileName();
    }

    if(fileProperty.fileName == "")
    {
        return ;
    }

    settingProperty->setGeometry(0,0,460,595);
    settingProperty->clearLineView();
    settingProperty->show();
    emit settingFileProperty(fileProperty);
}

//void ProgramFileView::on_file_treeView_doubleClicked(const QModelIndex &index)
//{
//    fileProperty.fileName = dirModel->fileInfo(index).fileName();

//    emit selectedFileChanged( fileProperty.fileName, index);
//}

void ProgramFileView::on_file_treeView_clicked(const QModelIndex &index)
{
    if(!GSOAP_OPERATE_PROGRAM_FILE_SWITCH)
    {
        fileProperty.fileName = dirModel->fileInfo(index).fileName();//fileName(index);
        emit selectedFileChanged( fileProperty.fileName, index);
    }
}

void ProgramFileView::on_pushButton_clicked()
{
    //手动刷新
    emit updateProgramFileList_Signal();
}

void ProgramFileView::changeLanguage(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/proFileViewEnglishLan.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
    ui->retranslateUi(this);

    copySettingDialog->changeLanguage();
    deleteDialog->changeLanguage();
    newfileView->changeLanguage();
    settingProperty->changeLanguage();
}

void ProgramFileView::on_RemoteFile_TableWidget_itemClicked(QTableWidgetItem *item)
{
    if(GSOAP_OPERATE_PROGRAM_FILE_SWITCH)
    {
        // 获取当前选择文件内容
        currentSelectFileName = item->text();
        fileProperty.fileName = currentSelectFileName;//fileName(index);

        emit selectedFileChanged( fileProperty.fileName, ui->RemoteFile_TableWidget->currentIndex());
    }
}
